package org.jetlinks.pro.tenant.term;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hswebframework.ezorm.core.param.Term;

import java.util.List;
import java.util.Map;

@Getter
@Setter
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
public class AssetsTenantTerm {

    private String assetType;
    private List<String> assetIds;
    private boolean not;

    public static AssetsTenantTerm of(String assetType, List<String> assetIds) {
        return of(assetType, assetIds, false);
    }

    public AssetsTenantTerm not(boolean not) {
        this.not = not;
        return this;
    }

    public static AssetsTenantTerm of(Term term) {
        Object value = term.getValue();
        if (value instanceof AssetsTenantTerm) {
            return ((AssetsTenantTerm) value);
        }
        JSONObject obj;
        if (value instanceof String) {
            obj = JSON.parseObject(String.valueOf(value));
        } else if (value instanceof Map) {
            obj = new JSONObject(((Map) value));
        } else {
            throw new UnsupportedOperationException("error.unsupported_term");
        }
        return obj.toJavaObject(AssetsTenantTerm.class);
    }

}
